package com.my.user.dao.impl;

import com.my.entity.User;
import com.my.user.dao.IUserDao;
import com.my.user.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

//数据访问层，原子性增删改查
public class UserDaoImpl implements IUserDao {
    private final String URL="jdbc:mysql://localhost:3306/jsppro";
    private final String USERNAME = "root";
    private final String PASSWORD = "12345678";

    public User queryUserByName(String name){
        User user = null;
        ResultSet rs = null;
        try{
            String sql = "select * from users where name=?";
            Object[] params={name};
            rs = DBUtil.executeQuery(sql,params);
            if(rs.next()){
                String uname = rs.getString("name");
                String upwd =rs.getString("pwd");
                user = new User(uname,upwd);
            }
            return user;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            DBUtil.closeAll(DBUtil.rs,DBUtil.pst,DBUtil.connection);
        }
    }

    public boolean isExist(String name){
        return queryUserByName(name)==null ? false:true;
    }

    public boolean addUser(User user){
        String sql="insert into users(name,pwd) values(?,?)";
        Object[] params = {user.getName(),user.getPwd()};
        return DBUtil.executeUpdate(sql,params);
    }

    public boolean deleteUserByName(String name){
       String sql="delete from users where name=?";
       Object[] params = {name};
       return DBUtil.executeUpdate(sql,params);
    }

    public boolean updateUserByName(User user){
       String sql = "update users set pwd=? where name=?";
       Object[] params = {user.getPwd(),user.getName()};
       return DBUtil.executeUpdate(sql,params);
    }

    public boolean pwdIsRight(User user){
        ResultSet rs = null;
        try{

            String sql = "select count(*) from users where name=? and pwd=?";
            Object[] params={user.getName(),user.getPwd()};
            rs = DBUtil.executeQuery(sql,params);
            int result = -1;
            if(rs.next()){
                result = rs.getInt(1);
            }
            if(result>0){
                return true;
            }
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            DBUtil.closeAll(DBUtil.rs,DBUtil.pst,DBUtil.connection);
        }
    }

    public List<User> queryAllUsers(){
        List<User> users = new ArrayList<User>();
        ResultSet rs = null;
        try{
            String sql = "select * from users";
            rs = DBUtil.executeQuery(sql,null);

            while(rs.next()){
                String name = rs.getString("name");
                String pwd = rs.getString("pwd");
                User user = new User(name,pwd);
                users.add(user);
            }
            return users;
        }catch (Exception e){
            e.printStackTrace();
            return users;
        }finally {
            DBUtil.closeAll(DBUtil.rs,DBUtil.pst,DBUtil.connection);
        }
    }

    @Override
    public int getTotalCount() {
        String sql = "select count(1) from users";
        return DBUtil.getTotalCount(sql);
    }

    @Override
    public List<User> queryUserByPage(int currentPage, int pageSize) {
        String sql = "select * from users limit ?,?";
        Object[] params = {(currentPage-1)*pageSize,pageSize};
        ResultSet rs = DBUtil.executeQuery(sql,params);
        List<User> users = new ArrayList<User>();
        try{
            while(rs.next()){
                String name = rs.getString("name");
                String pwd = rs.getString("pwd");
                User user = new User(name,pwd);
                users.add(user);
            }
            return users;
        }catch (SQLException e){
            e.printStackTrace();
            return users;
        }catch (Exception e){
            e.printStackTrace();
            return users;
        }finally {
            DBUtil.closeAll(DBUtil.rs,DBUtil.pst,DBUtil.connection);
        }
    }
}
